package com.atguigu.gulimall.cart.controller;

import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.gulimall.cart.interceptor.CartInterceptor;
import com.atguigu.gulimall.cart.service.CartService;
import com.atguigu.gulimall.cart.vo.Cart;
import com.atguigu.gulimall.cart.vo.CartItem;
import com.atguigu.gulimall.cart.vo.UserInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.xml.ws.Action;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author wangzhenjun
 * @date 2022/3/17 15:51
 */
@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 给远程gulimall-order调用
     */
    @ResponseBody
    @GetMapping("/currentUserItems")
    public List<CartItem> currentUserItems() {
        List<CartItem> items = cartService.getUserCartItems();
        return items;
    }

    /**
     * 参考京东，在点击购物车时，会为临时用户生成一个`name`为`user-key`的`cookie`临时标识，过期时间为一个月，
     * 如果手动清除`user-key`，那么临时购物车的购物项也被清除，所以`user-key`是用来标识和存储临时购物车数据的
     * @return
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {

        Cart cart = cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";
    }

    /**
     * 添加商品到购物车
     * 防止用户恶意刷新，可以使用重定向的办法，本方法不跳转页面，只是执行完业务代码后，跳转到别的方法，让那个方法跳转页面
     *      * redirectAttributes.addFlashAttribute() 将数据保存在session里面可以在页面取出，但是只能取一次
     *      * redirectAttributes.addAttribute("skuId", skuId); 将数据放在URL后面
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes attributes) throws ExecutionException, InterruptedException {

        cartService.addToCart(skuId,num);

        attributes.addAttribute("skuId",skuId);

        return "redirect:http://cart.gulimall.com/addCartSuccess.html";
    }

    /**
     * 跳转成功页
     * 获取购物项信息
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/addCartSuccess.html")
    public String addCartItemSuccess(@RequestParam("skuId") Long skuId,Model model) {
        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("item", cartItem);
        return "success";
    }
    /**
     * 勾选购物项目
     */
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId, @RequestParam("check") Integer check) {

        cartService.checkItem(skuId, check);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 修改购物项目的数量
     */
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num) {

        cartService.changeItemCount(skuId, num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 删除购物车里的某一项
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {

        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }
}
